Skip to content

Bug fix: prevents error "Cannot set headers after they are sent" in movies and lists routes#212

Open
ankaba-x00 wants to merge 1 commit into
safak:mern-netflix-appfrom
ankaba-x00:fix-verify-bug
Open

Bug fix: prevents error "Cannot set headers after they are sent" in movies and lists routes#212
ankaba-x00 wants to merge 1 commit into
safak:mern-netflix-appfrom
ankaba-x00:fix-verify-bug

Conversation

@ankaba-x00

@ankaba-x00 ankaba-x00 commented Oct 6, 2025

Copy link
Copy Markdown

Hello,

I found and fixed a bug in the mern-netflix-app branch where the backend was throwing an error due to a syntactical bug.

Problem

The backend is throwing ERR_HTTP_HEADERS_SENT errors when JWT token validation failed

Root Cause

The error occurs because your code is trying to send multiple responses for a single request due to a missing return keyword. Because of this missing return keyword, when a response error (403, "Token is invalid") is received, the route handler tries to send another response because it moves on to next().

Solution

Add return keyword:

  • BUGGY: if (err) res.status(403).json("Token is not valid!");
  • FIXED: if (err) return res.status(403).json("Token is not valid!");

Files Affected

  • routes/movies.js (line 83)
  • routes/lists.js (line 57)
  • All routes using the JWT verification middleware

Testing

  • verify that invalid tokens return proper 403 responses without server errors
  • check that multiple concurrent requests don't cause header conflicts

Notes

This fix does not change any database or frontend behavior; it only ensures the server handles responses correctly.

Have a good day,
Anna

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant